home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Utils / Software2 / Product11 / Setup.exe / MT-3.16-full-en_US / extlib / DateTime.pm < prev    next >
Text File  |  2004-04-19  |  75KB  |  2,714 lines

  1. package DateTime;
  2.  
  3. use strict;
  4.  
  5. use vars qw($VERSION);
  6.  
  7.  
  8. BEGIN
  9. {
  10.     $VERSION = '0.21';
  11.  
  12.     my $loaded = 0;
  13.     unless ( $ENV{PERL_DATETIME_PP} )
  14.     {
  15.     eval
  16.     {
  17.         if ( $] >= 5.006 )
  18.         {
  19.         require XSLoader;
  20.         XSLoader::load( 'DateTime', $DateTime::VERSION );
  21.         }
  22.         else
  23.         {
  24.         require DynaLoader;
  25.         @DateTime::ISA = 'DynaLoader';
  26.         DateTime->bootstrap( $DateTime::VERSION );
  27.         }
  28.     };
  29.  
  30.     die $@ if $@ && $@ !~ /object version/;
  31.  
  32.         $loaded = 1 unless $@;
  33.     }
  34.  
  35.     if ($loaded)
  36.     {
  37.         require DateTimePPExtra
  38.             unless defined &DateTime::_normalize_tai_seconds;
  39.     }
  40.     else
  41.     {
  42.         require DateTimePP;
  43.     }
  44. }
  45.  
  46. use DateTime::Duration;
  47. use DateTime::Locale;
  48. use DateTime::TimeZone;
  49. use Params::Validate qw( validate validate_pos SCALAR BOOLEAN HASHREF OBJECT );
  50. use Time::Local ();
  51.  
  52. # for some reason, overloading doesn't work unless fallback is listed
  53. # early.
  54. #
  55. # 3rd parameter ( $_[2] ) means the parameters are 'reversed'.
  56. # see: "Calling conventions for binary operations" in overload docs.
  57. #
  58. use overload ( 'fallback' => 1,
  59.                '<=>' => '_compare_overload',
  60.                'cmp' => '_compare_overload',
  61.                '""'  => 'iso8601',
  62.                '-'   => '_subtract_overload',
  63.                '+'   => '_add_overload',
  64.              );
  65.  
  66. # Have to load this after overloading is defined, after BEGIN blocks
  67. # or else weird crashes ensue
  68. require DateTime::Infinite;
  69.  
  70. use constant MAX_NANOSECONDS => 1_000_000_000;  # 1E9 = almost 32 bits
  71.  
  72. use constant INFINITY     =>       100 ** 100 ** 100 ;
  73. use constant NEG_INFINITY => -1 * (100 ** 100 ** 100);
  74. use constant NAN          => INFINITY - INFINITY;
  75.  
  76. my( @MonthLengths, @LeapYearMonthLengths );
  77.  
  78. BEGIN
  79. {
  80.     @MonthLengths =
  81.         ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
  82.  
  83.     @LeapYearMonthLengths = @MonthLengths;
  84.     $LeapYearMonthLengths[1]++;
  85. }
  86.  
  87. {
  88.     # I'd rather use Class::Data::Inheritable for this, but there's no
  89.     # way to add the module-loading behavior to an accessor it
  90.     # creates, despite what its docs say!
  91.     my $DefaultLocale;
  92.     sub DefaultLocale
  93.     {
  94.         my $class = shift;
  95.  
  96.         if (@_)
  97.         {
  98.             my $lang = shift;
  99.  
  100.             DateTime::Locale->load($lang);
  101.  
  102.             $DefaultLocale = $lang;
  103.         }
  104.  
  105.         return $DefaultLocale;
  106.     }
  107.     # backwards compat
  108.     *DefaultLanguage = \&DefaultLocale;
  109. }
  110. __PACKAGE__->DefaultLocale('en_US');
  111.  
  112. my $BasicValidate =
  113.     { year   => { type => SCALAR },
  114.       month  => { type => SCALAR, default => 1,
  115.                   callbacks =>
  116.                   { 'is between 1 and 12' =>
  117.                     sub { $_[0] >= 1 && $_[0] <= 12 }
  118.                   },
  119.                 },
  120.       day    => { type => SCALAR, default => 1,
  121.                   callbacks =>
  122.                   { 'is a possible valid day of month' =>
  123.                     sub { $_[0] >= 1 && $_[0] <= 31 }
  124.                   },
  125.                 },
  126.       hour   => { type => SCALAR, default => 0,
  127.                   callbacks =>
  128.                   { 'is between 0 and 23' =>
  129.                     sub { $_[0] >= 0 && $_[0] <= 23 },
  130.                   },
  131.                 },
  132.       minute => { type => SCALAR, default => 0,
  133.                   callbacks =>
  134.                   { 'is between 0 and 59' =>
  135.                     sub { $_[0] >= 0 && $_[0] <= 59 },
  136.                   },
  137.                 },
  138.       second => { type => SCALAR, default => 0,
  139.                   callbacks =>
  140.                   { 'is between 0 and 61' =>
  141.                     sub { $_[0] >= 0 && $_[0] <= 61 },
  142.                   },
  143.                 },
  144.       nanosecond => { type => SCALAR, default => 0,
  145.                       callbacks =>
  146.                       { 'cannot be negative' =>
  147.                         sub { $_[0] >= 0 },
  148.                       }
  149.                     },
  150.       locale    => { type => SCALAR | OBJECT,
  151.                      default => undef },
  152.       language  => { type => SCALAR | OBJECT,
  153.                      optional => 1 },
  154.     };
  155.  
  156. my $NewValidate =
  157.     { %$BasicValidate,
  158.       time_zone => { type => SCALAR | OBJECT,
  159.                      default => 'floating' },
  160.     };
  161.  
  162. sub new
  163. {
  164.     my $class = shift;
  165.     my %p = validate( @_, $NewValidate );
  166.  
  167.     die "Invalid day of month (day = $p{day} - month = $p{month})\n"
  168.         if $p{day} > $class->_month_length( $p{year}, $p{month} );
  169.  
  170.     my $self = {};
  171.  
  172.     $p{locale} = delete $p{language} if exists $p{language};
  173.     $p{locale} = $class->DefaultLocale unless defined $p{locale};
  174.  
  175.     if ( ref $p{locale} )
  176.     {
  177.         $self->{locale} = $p{locale};
  178.     }
  179.     else
  180.     {
  181.         $self->{locale} = DateTime::Locale->load( $p{locale} );
  182.     }
  183.  
  184.     $self->{tz} =
  185.         ( ref $p{time_zone} ?
  186.           $p{time_zone} :
  187.           DateTime::TimeZone->new( name => $p{time_zone} )
  188.         );
  189.  
  190.     $self->{local_rd_days} =
  191.         $class->_ymd2rd( @p{ qw( year month day ) } );
  192.  
  193.     $self->{local_rd_secs} =
  194.         $class->_time_as_seconds( @p{ qw( hour minute second ) } );
  195.  
  196.     $self->{rd_nanosecs} = $p{nanosecond};
  197.  
  198.     bless $self, $class;
  199.  
  200.     $self->_normalize_nanoseconds( $self->{local_rd_secs}, $self->{rd_nanosecs} );
  201.  
  202.     # Set this explicitly since it can't be calculated accurately
  203.     # without knowing our time zone offset, and it's possible that the
  204.     # offset can't be calculated without having at least a rough guess
  205.     # of the datetime's year.  This year need not be correct, as long
  206.     # as its equal or greater to the correct number, so we fudge by
  207.     # adding one to the local year given to the constructor.
  208.     $self->{utc_year} = $p{year} + 1;
  209.  
  210.     $self->_calc_utc_rd;
  211.     $self->_calc_local_rd;
  212.  
  213.     if ( $p{second} > 59 )
  214.     {
  215.         if ( $self->{tz}->is_floating ||
  216.              # If true, this means that the actual calculated leap
  217.              # second does not occur in the second given to new()
  218.              ( $self->{utc_rd_secs} - 86399
  219.                <
  220.                $p{second} - 59 )
  221.            )
  222.         {
  223.             die "Invalid second value ($p{second})\n";
  224.         }
  225.     }
  226.  
  227.     return $self;
  228. }
  229.  
  230. sub _calc_utc_rd
  231. {
  232.     my $self = shift;
  233.  
  234.     delete $self->{utc_c};
  235.  
  236.     if ( $self->{tz}->is_utc || $self->{tz}->is_floating )
  237.     {
  238.         $self->{utc_rd_days} = $self->{local_rd_days};
  239.         $self->{utc_rd_secs} = $self->{local_rd_secs};
  240.     }
  241.     else
  242.     {
  243.         $self->{utc_rd_days} = $self->{local_rd_days};
  244.         $self->{utc_rd_secs} =
  245.             $self->{local_rd_secs} - $self->_offset_from_local_time;
  246.     }
  247.  
  248.     $self->_normalize_seconds;
  249. }
  250.  
  251. sub _normalize_seconds
  252. {
  253.     my $self = shift;
  254.  
  255.     return if $self->{utc_rd_secs} >= 0 && $self->{utc_rd_secs} <= 86399;
  256.  
  257.     if ( $self->{tz}->is_floating )
  258.     {
  259.         $self->_normalize_tai_seconds( $self->{utc_rd_days}, $self->{utc_rd_secs} );
  260.     }
  261.     else
  262.     {
  263.         $self->_normalize_leap_seconds( $self->{utc_rd_days}, $self->{utc_rd_secs} );
  264.     }
  265. }
  266.  
  267. sub _calc_local_rd
  268. {
  269.     my $self = shift;
  270.  
  271.     delete $self->{local_c};
  272.  
  273.     # We must short circuit for UTC times or else we could end up with
  274.     # loops between DateTime.pm and DateTime::TimeZone
  275.     if ( $self->{tz}->is_utc || $self->{tz}->is_floating )
  276.     {
  277.         $self->{local_rd_days} = $self->{utc_rd_days};
  278.         $self->{local_rd_secs} = $self->{utc_rd_secs};
  279.     }
  280.     else
  281.     {
  282.         $self->{local_rd_days} = $self->{utc_rd_days};
  283.         $self->{local_rd_secs} = $self->{utc_rd_secs} + $self->offset;
  284.  
  285.         # intentionally ignore leap seconds here
  286.         $self->_normalize_tai_seconds( $self->{local_rd_days}, $self->{local_rd_secs} );
  287.     }
  288.  
  289.     $self->_calc_local_components;
  290. }
  291.  
  292. sub _calc_local_components
  293. {
  294.     my $self = shift;
  295.  
  296.     @{ $self->{local_c} }{ qw( year month day day_of_week
  297.                                day_of_year quarter day_of_quarter) } =
  298.         $self->_rd2ymd( $self->{local_rd_days}, 1 );
  299.  
  300.     @{ $self->{local_c} }{ qw( hour minute second ) } =
  301.         $self->_seconds_as_components( $self->{local_rd_secs}, $self->{utc_rd_secs} );
  302. }
  303.  
  304. sub _calc_utc_components
  305. {
  306.     my $self = shift;
  307.  
  308.     $self->_calc_utc_rd unless defined $self->{utc_rd_days};
  309.  
  310.     @{ $self->{utc_c} }{ qw( year month day ) } =
  311.         $self->_rd2ymd( $self->{utc_rd_days} );
  312.  
  313.     @{ $self->{utc_c} }{ qw( hour minute second ) } =
  314.         $self->_seconds_as_components( $self->{utc_rd_secs} );
  315. }
  316.  
  317. sub _utc_ymd
  318. {
  319.     my $self = shift;
  320.  
  321.     $self->_calc_utc_components unless exists $self->{utc_c}{year};
  322.  
  323.     return @{ $self->{utc_c} }{ qw( year month day ) };
  324. }
  325.  
  326. sub _utc_hms
  327. {
  328.     my $self = shift;
  329.  
  330.     $self->_calc_utc_components unless exists $self->{utc_c}{hour};
  331.  
  332.     return @{ $self->{utc_c} }{ qw( hour minute second ) };
  333. }
  334.  
  335. sub from_epoch
  336. {
  337.     my $class = shift;
  338.     my %p = validate( @_,
  339.                       { epoch => { type => SCALAR },
  340.                         locale     => { type => SCALAR | OBJECT, optional => 1 },
  341.                         language   => { type => SCALAR | OBJECT, optional => 1 },
  342.                         time_zone  => { type => SCALAR | OBJECT, optional => 1 },
  343.                       }
  344.                     );
  345.  
  346.     my %args;
  347.  
  348.     # Because epoch may come from Time::HiRes
  349.     my $fraction = $p{epoch} - int( $p{epoch} );
  350.     $args{nanosecond} = int( $fraction * MAX_NANOSECONDS )
  351.         if $fraction;
  352.  
  353.     # Note, for very large negative values this may give a blatantly
  354.     # wrong answer.
  355.     @args{ qw( second minute hour day month year ) } =
  356.         ( gmtime( int delete $p{epoch} ) )[ 0..5 ];
  357.     $args{year} += 1900;
  358.     $args{month}++;
  359.  
  360.     my $self = $class->new( %p, %args, time_zone => 'UTC' );
  361.  
  362.     $self->set_time_zone( $p{time_zone} ) if exists $p{time_zone};
  363.  
  364.     return $self;
  365. }
  366.  
  367. # use scalar time in case someone's loaded Time::Piece
  368. sub now { shift->from_epoch( epoch => (scalar time), @_ ) }
  369.  
  370. sub today { shift->now(@_)->truncate( to => 'day' ) }
  371.  
  372. sub from_object
  373. {
  374.     my $class = shift;
  375.     my %p = validate( @_,
  376.                       { object => { type => OBJECT,
  377.                                     can => 'utc_rd_values',
  378.                                   },
  379.                         locale     => { type => SCALAR | OBJECT, optional => 1 },
  380.                         language   => { type => SCALAR | OBJECT, optional => 1 },
  381.                       },
  382.                     );
  383.  
  384.     my $object = delete $p{object};
  385.  
  386.     my ( $rd_days, $rd_secs, $rd_nanosecs ) = $object->utc_rd_values;
  387.  
  388.     # A kludge because until all calendars are updated to return all
  389.     # three values, $rd_nanosecs could be undef
  390.     $rd_nanosecs ||= 0;
  391.  
  392.     my %args;
  393.     @args{ qw( year month day ) } = $class->_rd2ymd($rd_days);
  394.     @args{ qw( hour minute second ) } = $class->_seconds_as_components($rd_secs);
  395.     $args{nanosecond} = $rd_nanosecs;
  396.  
  397.     my $new = $class->new( %p, %args, time_zone => 'UTC' );
  398.  
  399.     if ( $object->can('time_zone') )
  400.     {
  401.         $new->set_time_zone( $object->time_zone );
  402.     }
  403.     else
  404.     {
  405.         $new->set_time_zone( 'floating' );
  406.     }
  407.  
  408.     return $new;
  409. }
  410.  
  411. my $LastDayOfMonthValidate = { %$NewValidate };
  412. foreach ( keys %$LastDayOfMonthValidate )
  413. {
  414.     my %copy = %{ $LastDayOfMonthValidate->{$_} };
  415.  
  416.     delete $copy{default};
  417.     $copy{optional} = 1 unless $_ eq 'year' || $_ eq 'month';
  418.  
  419.     $LastDayOfMonthValidate->{$_} = \%copy;
  420. }
  421.  
  422. sub last_day_of_month
  423. {
  424.     my $class = shift;
  425.     my %p = validate( @_, $LastDayOfMonthValidate );
  426.  
  427.     my $day = $class->_month_length( $p{year}, $p{month} );
  428.  
  429.     return $class->new( %p, day => $day );
  430. }
  431.  
  432. sub _month_length
  433. {
  434.     return ( $_[0]->_is_leap_year( $_[1] ) ?
  435.              $LeapYearMonthLengths[ $_[2] - 1 ] :
  436.              $MonthLengths[ $_[2] - 1 ]
  437.            );
  438. }
  439.  
  440. my $FromDayOfYearValidate = { %$NewValidate };
  441. foreach ( keys %$FromDayOfYearValidate )
  442. {
  443.     next if $_ eq 'month' || $_ eq 'day';
  444.  
  445.     my %copy = %{ $FromDayOfYearValidate->{$_} };
  446.  
  447.     delete $copy{default};
  448.     $copy{optional} = 1 unless $_ eq 'year' || $_ eq 'month';
  449.  
  450.     $FromDayOfYearValidate->{$_} = \%copy;
  451. }
  452. $FromDayOfYearValidate->{day_of_year} =
  453.     { type => SCALAR,
  454.       callbacks =>
  455.       { 'is between 1 and 366' =>
  456.         sub { $_[0] >= 1 && $_[0] <= 366 }
  457.       }
  458.     };
  459. sub from_day_of_year
  460. {
  461.     my $class = shift;
  462.     my %p = validate( @_, $FromDayOfYearValidate );
  463.  
  464.     my $is_leap_year = $class->_is_leap_year( $p{year} );
  465.  
  466.     die "$p{year} is not a leap year.\n"
  467.         if $p{day_of_year} == 366 && ! $is_leap_year;
  468.  
  469.     my $month = 1;
  470.     my $day = delete $p{day_of_year};
  471.  
  472.     while ( $month <= 12 && $day > $class->_month_length( $p{year}, $month ) )
  473.     {
  474.         $day -= $class->_month_length( $p{year}, $month );
  475.         $month++;
  476.     }
  477.  
  478.     return DateTime->new( %p,
  479.                           month => $month,
  480.                           day   => $day,
  481.                         );
  482. }
  483.  
  484. sub clone { bless { %{ $_[0] } }, ref $_[0] }
  485.  
  486. sub year    { $_[0]->{local_c}{year} }
  487.  
  488. sub ce_year { $_[0]->{local_c}{year} <= 0 ?
  489.               $_[0]->{local_c}{year} - 1 :
  490.               $_[0]->{local_c}{year} }
  491.  
  492. sub era { $_[0]->ce_year > 0 ? 'CE' : 'BCE' }
  493.  
  494. sub year_with_era { (abs $_[0]->ce_year) . $_[0]->era }
  495.  
  496. sub month   { $_[0]->{local_c}{month} }
  497. *mon = \&month;
  498.  
  499. sub month_0 { $_[0]->{local_c}{month} - 1 };
  500. *mon_0 = \&month_0;
  501.  
  502. sub month_name { $_[0]->{locale}->month_name( $_[0] ) }
  503.  
  504. sub month_abbr { $_[0]->{locale}->month_abbreviation( $_[0] ) }
  505.  
  506. sub day_of_month { $_[0]->{local_c}{day} }
  507. *day  = \&day_of_month;
  508. *mday = \&day_of_month;
  509.  
  510. sub weekday_of_month { use integer; ( ( $_[0]->day - 1 ) / 7 ) + 1 }
  511.  
  512. sub quarter {$_[0]->{local_c}{quarter} };
  513.  
  514. sub day_of_month_0 { $_[0]->{local_c}{day} - 1 }
  515. *day_0  = \&day_of_month_0;
  516. *mday_0 = \&day_of_month_0;
  517.  
  518. sub day_of_week { $_[0]->{local_c}{day_of_week} }
  519. *wday = \&day_of_week;
  520. *dow  = \&day_of_week;
  521.  
  522. sub day_of_week_0 { $_[0]->{local_c}{day_of_week} - 1 }
  523. *wday_0 = \&day_of_week_0;
  524. *dow_0  = \&day_of_week_0;
  525.  
  526. sub day_name { $_[0]->{locale}->day_name( $_[0] ) }
  527.  
  528. sub day_abbr { $_[0]->{locale}->day_abbreviation( $_[0] ) }
  529.  
  530. sub day_of_quarter { $_[0]->{local_c}{day_of_quarter} }
  531. *doq = \&day_of_quarter;
  532.  
  533. sub day_of_quarter_0 { $_[0]->day_of_quarter - 1 }
  534. *doq_0 = \&day_of_quarter_0;
  535.  
  536. sub day_of_year { $_[0]->{local_c}{day_of_year} }
  537. *doy = \&day_of_year;
  538.  
  539. sub day_of_year_0 { $_[0]->{local_c}{day_of_year} - 1 }
  540. *doy_0 = \&day_of_year_0;
  541.  
  542. sub ymd
  543. {
  544.     my ( $self, $sep ) = @_;
  545.     $sep = '-' unless defined $sep;
  546.  
  547.     return sprintf( "%0.4d%s%0.2d%s%0.2d",
  548.                     $self->year, $sep,
  549.                     $self->{local_c}{month}, $sep,
  550.                     $self->{local_c}{day} );
  551. }
  552. *date = \&ymd;
  553.  
  554. sub mdy
  555. {
  556.     my ( $self, $sep ) = @_;
  557.     $sep = '-' unless defined $sep;
  558.  
  559.     return sprintf( "%0.2d%s%0.2d%s%0.4d",
  560.                     $self->{local_c}{month}, $sep,
  561.                     $self->{local_c}{day}, $sep,
  562.                     $self->year );
  563. }
  564.  
  565. sub dmy
  566. {
  567.     my ( $self, $sep ) = @_;
  568.     $sep = '-' unless defined $sep;
  569.  
  570.     return sprintf( "%0.2d%s%0.2d%s%0.4d",
  571.                     $self->{local_c}{day}, $sep,
  572.                     $self->{local_c}{month}, $sep,
  573.                     $self->year );
  574. }
  575.  
  576. sub hour   { $_[0]->{local_c}{hour} }
  577. sub hour_1 { $_[0]->{local_c}{hour} + 1 }
  578.  
  579. sub hour_12   { my $h = $_[0]->hour % 12; return $h ? $h : 12 }
  580. sub hour_12_0 { $_[0]->hour % 12 }
  581.  
  582. sub minute { $_[0]->{local_c}{minute} }
  583. *min = \&minute;
  584.  
  585. sub second { $_[0]->{local_c}{second} }
  586. *sec = \&second;
  587.  
  588. sub fractional_second { $_[0]->second + $_[0]->nanosecond / MAX_NANOSECONDS }
  589.  
  590. sub nanosecond { $_[0]->{rd_nanosecs} }
  591.  
  592. sub millisecond { _round( $_[0]->{rd_nanosecs} / 1000000 ) }
  593.  
  594. sub microsecond { _round( $_[0]->{rd_nanosecs} / 1000 ) }
  595.  
  596. sub _round
  597. {
  598.     my $val = shift;
  599.     my $int = int $val;
  600.  
  601.     return $val - $int >= 0.5 ? $int + 1 : $int;
  602. }
  603.  
  604. sub leap_seconds
  605. {
  606.     my $self = shift;
  607.  
  608.     return 0 if $self->{tz}->is_floating;
  609.  
  610.     return DateTime->_leap_seconds( $self->{utc_rd_days} );
  611. }
  612.  
  613. sub hms
  614. {
  615.     my ( $self, $sep ) = @_;
  616.     $sep = ':' unless defined $sep;
  617.  
  618.     return sprintf( "%0.2d%s%0.2d%s%0.2d",
  619.                     $self->{local_c}{hour}, $sep,
  620.                     $self->{local_c}{minute}, $sep,
  621.                     $self->{local_c}{second} );
  622. }
  623. # don't want to override CORE::time()
  624. *DateTime::time = \&hms;
  625.  
  626. sub iso8601 { join 'T', $_[0]->ymd('-'), $_[0]->hms(':') }
  627. *datetime = \&iso8601;
  628.  
  629. sub is_leap_year { $_[0]->_is_leap_year( $_[0]->year ) }
  630.  
  631. sub week
  632. {
  633.     my $self = shift;
  634.  
  635.     unless ( defined $self->{local_c}{week_year} )
  636.     {
  637.         # This algorithm was taken from Date::Calc's DateCalc.c file
  638.         my $jan_one_dow_m1 =
  639.             ( ( $self->_ymd2rd( $self->year, 1, 1 ) + 6 ) % 7 );
  640.  
  641.         $self->{local_c}{week_number} =
  642.             int( ( ( $self->day_of_year - 1 ) + $jan_one_dow_m1 ) / 7 );
  643.         $self->{local_c}{week_number}++ if $jan_one_dow_m1 < 4;
  644.  
  645.         if ( $self->{local_c}{week_number} == 0 )
  646.         {
  647.             $self->{local_c}{week_year} = $self->year - 1;
  648.             $self->{local_c}{week_number} =
  649.                 $self->_weeks_in_year( $self->{local_c}{week_year} );
  650.         }
  651.         elsif ( $self->{local_c}{week_number} == 53 &&
  652.                 $self->_weeks_in_year( $self->year ) == 52 )
  653.         {
  654.             $self->{local_c}{week_number} = 1;
  655.             $self->{local_c}{week_year} = $self->year + 1;
  656.         }
  657.         else
  658.         {
  659.             $self->{local_c}{week_year} = $self->year;
  660.         }
  661.     }
  662.  
  663.     return @{ $self->{local_c} }{ 'week_year', 'week_number' }
  664. }
  665.  
  666. # Also from DateCalc.c
  667. sub _weeks_in_year
  668. {
  669.     my $self = shift;
  670.     my $year = shift;
  671.  
  672.     my $jan_one_dow =
  673.         ( ( $self->_ymd2rd( $year, 1, 1 ) + 6 ) % 7 ) + 1;
  674.     my $dec_31_dow =
  675.         ( ( $self->_ymd2rd( $year, 12, 31 ) + 6 ) % 7 ) + 1;
  676.  
  677.     return $jan_one_dow == 4 || $dec_31_dow == 4 ? 53 : 52;
  678. }
  679.  
  680. sub week_year   { ($_[0]->week)[0] }
  681. sub week_number { ($_[0]->week)[1] }
  682.  
  683. # ISO says that the first week of a year is the first week containing
  684. # a Thursday.  Extending that says that the first week of the month is
  685. # the first week containing a Thursday.  ICU agrees.
  686. #
  687. # Algorithm supplied by Rick Measham, who doesn't understand how it
  688. # works.  Neither do I.  Please feel free to explain this to me!
  689. sub week_of_month
  690. {
  691.     my $self = shift;
  692.  
  693.     # Faster than cloning just to get the dow
  694.     my $first_wday_of_month = ( 8 - ( $self->day - $self->dow ) % 7 ) % 7;
  695.     $first_wday_of_month = 7 unless $first_wday_of_month;
  696.  
  697.     my $wom = int( ( $self->day + $first_wday_of_month - 2 ) / 7 );
  698.     return ( $first_wday_of_month <= 4 ) ? $wom + 1 : $wom;
  699. }
  700.  
  701. sub time_zone { $_[0]->{tz} }
  702.  
  703. sub offset { $_[0]->{tz}->offset_for_datetime( $_[0] ) }
  704. sub _offset_from_local_time { $_[0]->{tz}->offset_for_local_datetime( $_[0] ) }
  705.  
  706. sub is_dst { $_[0]->{tz}->is_dst_for_datetime( $_[0] ) }
  707.  
  708. sub time_zone_long_name  { $_[0]->{tz}->name }
  709. sub time_zone_short_name { $_[0]->{tz}->short_name_for_datetime( $_[0] ) }
  710.  
  711. sub locale { $_[0]->{locale} }
  712. *language = \&locale;
  713.  
  714. sub utc_rd_values { @{ $_[0] }{ 'utc_rd_days', 'utc_rd_secs', 'rd_nanosecs' } }
  715.  
  716. # NOTE: no nanoseconds, no leap seconds
  717. sub utc_rd_as_seconds   { ( $_[0]->{utc_rd_days} * 86400 ) + $_[0]->{utc_rd_secs} }
  718.  
  719. # NOTE: no nanoseconds, no leap seconds
  720. sub local_rd_as_seconds { ( $_[0]->{local_rd_days} * 86400 ) + $_[0]->{local_rd_secs} }
  721.  
  722. # RD 1 is JD 1,721,424.5 - a simple offset
  723. sub jd
  724. {
  725.     my $self = shift;
  726.  
  727.     my $jd = $self->{utc_rd_days} + 1_721_424.5;
  728.  
  729.     my $day_length = $self->_day_length( $self->{utc_rd_days} );
  730.  
  731.     return ( $jd +
  732.              ( $self->{utc_rd_secs} / $day_length )  +
  733.              ( $self->{rd_nanosecs} / $day_length / MAX_NANOSECONDS )
  734.            );
  735. }
  736.  
  737. sub mjd { $_[0]->jd - 2_400_000.5 }
  738.  
  739. my %formats =
  740.     ( 'a' => sub { $_[0]->day_abbr },
  741.       'A' => sub { $_[0]->day_name },
  742.       'b' => sub { $_[0]->month_abbr },
  743.       'B' => sub { $_[0]->month_name },
  744.       'c' => sub { $_[0]->strftime( $_[0]->{locale}->default_datetime_format ) },
  745.       'C' => sub { int( $_[0]->year / 100 ) },
  746.       'd' => sub { sprintf( '%02d', $_[0]->day_of_month ) },
  747.       'D' => sub { $_[0]->strftime( '%m/%d/%y' ) },
  748.       'e' => sub { sprintf( '%2d', $_[0]->day_of_month ) },
  749.       'F' => sub { $_[0]->ymd('-') },
  750.       'g' => sub { substr( $_[0]->week_year, -2 ) },
  751.       'G' => sub { $_[0]->week_year },
  752.       'H' => sub { sprintf( '%02d', $_[0]->hour ) },
  753.       'I' => sub { sprintf( '%02d', $_[0]->hour_12 ) },
  754.       'j' => sub { $_[0]->day_of_year },
  755.       'k' => sub { sprintf( '%2d', $_[0]->hour ) },
  756.       'l' => sub { sprintf( '%2d', $_[0]->hour_12 ) },
  757.       'm' => sub { sprintf( '%02d', $_[0]->month ) },
  758.       'M' => sub { sprintf( '%02d', $_[0]->minute ) },
  759.       'n' => sub { "\n" }, # should this be OS-sensitive?
  760.       'N' => \&_format_nanosecs,
  761.       'p' => sub { $_[0]->{locale}->am_pm( $_[0] ) },
  762.       'P' => sub { lc $_[0]->{locale}->am_pm( $_[0] ) },
  763.       'r' => sub { $_[0]->strftime( '%I:%M:%S %p' ) },
  764.       'R' => sub { $_[0]->strftime( '%H:%M' ) },
  765.       's' => sub { $_[0]->epoch },
  766.       'S' => sub { sprintf( '%02d', $_[0]->second ) },
  767.       't' => sub { "\t" },
  768.       'T' => sub { $_[0]->strftime( '%H:%M:%S' ) },
  769.       'u' => sub { $_[0]->day_of_week },
  770.       # algorithm from Date::Format::wkyr
  771.       'U' => sub { my $dow = $_[0]->day_of_week;
  772.                    $dow = 0 if $dow == 7; # convert to 0-6, Sun-Sat
  773.                    my $doy = $_[0]->day_of_year - 1;
  774.                    return int( ( $doy - $dow + 13 ) / 7 - 1 )
  775.                  },
  776.       'V' => sub { sprintf( '%02d', $_[0]->week_number ) },
  777.       'w' => sub { my $dow = $_[0]->day_of_week;
  778.                    return $dow % 7;
  779.                  },
  780.       'W' => sub { my $dow = $_[0]->day_of_week;
  781.                    my $doy = $_[0]->day_of_year - 1;
  782.                    return int( ( $doy - $dow + 13 ) / 7 - 1 )
  783.                  },
  784.       'x' => sub { $_[0]->strftime( $_[0]->{locale}->default_date_format ) },
  785.       'X' => sub { $_[0]->strftime( $_[0]->{locale}->default_time_format ) },
  786.       'y' => sub { sprintf( '%02d', substr( $_[0]->year, -2 ) ) },
  787.       'Y' => sub { return $_[0]->year },
  788.       'z' => sub { DateTime::TimeZone::offset_as_string( $_[0]->offset ) },
  789.       'Z' => sub { $_[0]->{tz}->short_name_for_datetime( $_[0] ) },
  790.       '%' => sub { '%' },
  791.     );
  792.  
  793. $formats{h} = $formats{b};
  794.  
  795. sub strftime
  796. {
  797.     my $self = shift;
  798.     # make a copy or caller's scalars get munged
  799.     my @formats = @_;
  800.  
  801.     my @r;
  802.     foreach my $f (@formats)
  803.     {
  804.         $f =~ s/
  805.                 %{(\w+)}
  806.                /
  807.                 $self->can($1) ? $self->$1() : "\%{$1}"
  808.                /sgex;
  809.  
  810.         # regex from Date::Format - thanks Graham!
  811.        $f =~ s/
  812.                 %([%a-zA-Z])
  813.                /
  814.                 $formats{$1} ? $formats{$1}->($self) : "\%$1"
  815.                /sgex;
  816.  
  817.         # %3N
  818.         $f =~ s/
  819.                 %(\d+)N
  820.                /
  821.                 $formats{N}->($self, $1)
  822.                /sgex;
  823.  
  824.         return $f unless wantarray;
  825.  
  826.         push @r, $f;
  827.     }
  828.  
  829.     return @r;
  830. }
  831.  
  832. sub _format_nanosecs
  833. {
  834.     my $self = shift;
  835.     my $precision = shift;
  836.  
  837.     my $ret = sprintf( "%09d", $self->{rd_nanosecs} );
  838.     return $ret unless $precision;   # default = 9 digits
  839.  
  840.     # rd_nanosecs might contain a fractional separator
  841.     my ( $int, $frac ) = split /[.,]/, $self->{rd_nanosecs};
  842.     $ret .= $frac if $frac;
  843.  
  844.     return substr( $ret, 0, $precision );
  845. }
  846.  
  847. sub epoch
  848. {
  849.     my $self = shift;
  850.  
  851.     return $self->{utc_c}{epoch}
  852.         if exists $self->{utc_c}{epoch};
  853.  
  854.     my ( $year, $month, $day ) = $self->_utc_ymd;
  855.     my @hms = $self->_utc_hms;
  856.  
  857.     $self->{utc_c}{epoch} =
  858.         eval { Time::Local::timegm_nocheck( ( reverse @hms ),
  859.                                             $day,
  860.                                             $month - 1,
  861.                                             $year,
  862.                                           ) };
  863.  
  864.     return $self->{utc_c}{epoch};
  865. }
  866.  
  867. sub hires_epoch
  868. {
  869.     my $self = shift;
  870.  
  871.     my $epoch = $self->epoch;
  872.  
  873.     return undef unless defined $epoch;
  874.  
  875.     my $nano = $self->{rd_nanosecs} / MAX_NANOSECONDS;
  876.  
  877.     return $epoch + $nano;
  878. }
  879.  
  880. sub is_finite { 1 }
  881. sub is_infinite { 0 }
  882.  
  883. # added for benefit of DateTime::TimeZone
  884. sub utc_year { $_[0]->{utc_year} }
  885.  
  886. # returns a result that is relative to the first datetime
  887. sub subtract_datetime
  888. {
  889.     my $self = shift;
  890.     my $dt = shift;
  891.  
  892.     # We only want a negative duration if $dt > $self.
  893.     my ( $bigger, $smaller, $negative ) =
  894.         ( $self >= $dt ?
  895.           ( $self, $dt, 0 ) :
  896.           ( $dt, $self, 1 )
  897.         );
  898.  
  899.     my $is_floating = $self->time_zone->is_floating &&
  900.                       $dt->time_zone->is_floating;
  901.  
  902.     my $minute_length = 60;
  903.  
  904.     unless ($is_floating)
  905.     {
  906.         my ( $utc_rd_days, $utc_rd_secs ) = $smaller->utc_rd_values;
  907.  
  908.         if ( $utc_rd_secs >= 86340 && ! $is_floating )
  909.         {
  910.             # If the smaller of the two datetimes occurs in the last
  911.             # UTC minute of the UTC day, then that minute may not be
  912.             # 60 seconds long.  If we need to subtract a minute from
  913.             # the larger datetime's minutes count in order to adjust
  914.             # the seconds difference to be positive, we need to know
  915.             # how long that minute was.  If one of the datetimes is
  916.             # floating, we just assume a minute is 60 seconds.
  917.  
  918.             $minute_length = $self->_day_length($utc_rd_days) - 86340;
  919.         }
  920.     }
  921.  
  922.     my ( $months, $days, $minutes, $seconds, $nanoseconds ) =
  923.         $self->_adjust_for_positive_difference
  924.             ( $bigger->year * 12 + $bigger->month, $smaller->year * 12 + $smaller->month,
  925.  
  926.               $bigger->day, $smaller->day,
  927.  
  928.               $bigger->hour * 60 + $bigger->minute, $smaller->hour * 60 + $smaller->minute,
  929.  
  930.           $bigger->second, $smaller->second,
  931.  
  932.           $bigger->nanosecond, $smaller->nanosecond,
  933.  
  934.           $minute_length,
  935.  
  936.           $self->_month_length( $smaller->year, $smaller->month ),
  937.             );
  938.  
  939.     if ($negative)
  940.     {
  941.         for ( $months, $days, $minutes, $seconds, $nanoseconds )
  942.         {
  943.         # Some versions of Perl can end up with -0 if we do "0 * -1"!!
  944.             $_ *= -1 if $_;
  945.         }
  946.     }
  947.  
  948.     return
  949.         DateTime::Duration->new
  950.             ( months      => $months,
  951.           days        => $days,
  952.           minutes     => $minutes,
  953.               seconds     => $seconds,
  954.               nanoseconds => $nanoseconds,
  955.             );
  956. }
  957.  
  958. sub _adjust_for_positive_difference
  959. {
  960.     my ( $self,
  961.      $month1, $month2,
  962.      $day1, $day2,
  963.      $min1, $min2,
  964.      $sec1, $sec2,
  965.      $nano1, $nano2,
  966.      $minute_length,
  967.      $month_length,
  968.        ) = @_;
  969.  
  970.     if ( $nano1 < $nano2 )
  971.     {
  972.         $sec1--;
  973.         $nano1 += MAX_NANOSECONDS;
  974.     }
  975.  
  976.     if ( $sec1 < $sec2 )
  977.     {
  978.         $min1--;
  979.         $sec1 += $minute_length;
  980.     }
  981.  
  982.     # A day always has 24 * 60 minutes, though the minutes may vary in
  983.     # length.
  984.     if ( $min1 < $min2 )
  985.     {
  986.     $day1--;
  987.     $min1 += 24 * 60;
  988.     }
  989.  
  990.     if ( $day1 < $day2 )
  991.     {
  992.     $month1--;
  993.     $day1 += $month_length;
  994.     }
  995.  
  996.     return ( $month1 - $month2,
  997.          $day1 - $day2,
  998.          $min1 - $min2,
  999.              $sec1 - $sec2,
  1000.              $nano1 - $nano2,
  1001.            );
  1002. }
  1003.  
  1004. sub subtract_datetime_absolute
  1005. {
  1006.     my $self = shift;
  1007.     my $dt = shift;
  1008.  
  1009.     my $utc_rd_secs1 = $self->utc_rd_as_seconds;
  1010.     $utc_rd_secs1 += DateTime->_leap_seconds( $self->{utc_rd_days} )
  1011.     if ! $self->time_zone->is_floating;
  1012.  
  1013.     my $utc_rd_secs2 = $dt->utc_rd_as_seconds;
  1014.     $utc_rd_secs2 += DateTime->_leap_seconds( $dt->{utc_rd_days} )
  1015.     if ! $dt->time_zone->is_floating;
  1016.  
  1017.     my $seconds = $utc_rd_secs1 - $utc_rd_secs2;
  1018.     my $nanoseconds = $self->nanosecond - $dt->nanosecond;
  1019.  
  1020.     if ( $nanoseconds < 0 )
  1021.     {
  1022.     $seconds--;
  1023.     $nanoseconds += MAX_NANOSECONDS;
  1024.     }
  1025.  
  1026.     return
  1027.         DateTime::Duration->new
  1028.             ( seconds     => $seconds,
  1029.               nanoseconds => $nanoseconds,
  1030.             );
  1031. }
  1032.  
  1033. sub delta_md
  1034. {
  1035.     my $self = shift;
  1036.     my $dt = shift;
  1037.  
  1038.     my $dur = $self->subtract_datetime($dt);
  1039.  
  1040.     return DateTime::Duration->new( months => $dur->delta_months,
  1041.                                     days   => $dur->delta_days );
  1042. }
  1043.  
  1044. sub delta_days { DateTime::Duration->new( days => int( $_[0]->jd - $_[1]->jd ) ) }
  1045.  
  1046. sub delta_ms
  1047. {
  1048.     my $self = shift;
  1049.     my $dt = shift;
  1050.  
  1051.     my $days = abs( int( $self->jd - $dt->jd ) );
  1052.  
  1053.     my $dur = $self->subtract_datetime($dt);
  1054.  
  1055.     my %p;
  1056.     $p{hours}   = $dur->hours + ( $days * 24 );
  1057.     $p{minutes} = $dur->minutes;
  1058.     $p{seconds} = $dur->seconds;
  1059.  
  1060.     if ( $self < $dt )
  1061.     {
  1062.         for ( qw( hours minutes seconds ) )
  1063.         {
  1064.             $p{$_} *= -1 if $p{$_};
  1065.         }
  1066.     }
  1067.  
  1068.     return DateTime::Duration->new(%p);
  1069. }
  1070.  
  1071. sub _add_overload
  1072. {
  1073.     my ( $dt, $dur, $reversed ) = @_;
  1074.  
  1075.     if ($reversed)
  1076.     {
  1077.         ( $dur, $dt ) = ( $dt, $dur );
  1078.     }
  1079.  
  1080.     # how to handle non duration objects?
  1081.  
  1082.     my $new = $dt->clone;
  1083.     $new->add_duration($dur);
  1084.     return $new;
  1085. }
  1086.  
  1087. sub _subtract_overload
  1088. {
  1089.     my ( $date1, $date2, $reversed ) = @_;
  1090.  
  1091.     if ($reversed)
  1092.     {
  1093.         ( $date2, $date1 ) = ( $date1, $date2 );
  1094.     }
  1095.  
  1096.     if ( UNIVERSAL::isa( $date2, 'DateTime::Duration' ) )
  1097.     {
  1098.         my $new = $date1->clone;
  1099.         $new->add_duration( $date2->inverse );
  1100.         return $new;
  1101.     }
  1102.     else
  1103.     {
  1104.         return $date1->subtract_datetime($date2);
  1105.     }
  1106.     # handle other cases?
  1107. }
  1108.  
  1109. sub add { return shift->add_duration( DateTime::Duration->new(@_) ) }
  1110.  
  1111. sub subtract { return shift->subtract_duration( DateTime::Duration->new(@_) ) }
  1112.  
  1113. sub subtract_duration { return $_[0]->add_duration( $_[1]->inverse ) }
  1114.  
  1115. sub add_duration
  1116. {
  1117.     my $self = shift;
  1118.     my ($dur) = validate_pos( @_, { isa => 'DateTime::Duration' } );
  1119.  
  1120.     # simple optimization
  1121.     return $self if $dur->is_zero;
  1122.  
  1123.     my %deltas = $dur->deltas;
  1124.  
  1125.     # This bit isn't quite right since DateTime::Infinite::Future -
  1126.     # infinite duration should NaN
  1127.     foreach my $val ( values %deltas )
  1128.     {
  1129.         my $inf;
  1130.         if ( $val == INFINITY )
  1131.         {
  1132.             $inf = DateTime::Infinite::Future->new;
  1133.         }
  1134.         elsif ( $val == NEG_INFINITY )
  1135.         {
  1136.             $inf = DateTime::Infinite::Past->new;
  1137.         }
  1138.  
  1139.         if ($inf)
  1140.         {
  1141.             %$self = %$inf;
  1142.             bless $self, ref $inf;
  1143.  
  1144.             return $self;
  1145.         }
  1146.     }
  1147.  
  1148.     return $self if $self->is_infinite;
  1149.  
  1150.     $self->{local_rd_days} += $deltas{days} if $deltas{days};
  1151.  
  1152.     if ( $deltas{months} )
  1153.     {
  1154.         # For preserve mode, if it is the last day of the month, make
  1155.         # it the 0th day of the following month (which then will
  1156.         # normalize back to the last day of the new month).
  1157.         my ($y, $m, $d) = ( $dur->is_preserve_mode ?
  1158.                             $self->_rd2ymd( $self->{local_rd_days} + 1 ) :
  1159.                             $self->_rd2ymd( $self->{local_rd_days} )
  1160.                           );
  1161.  
  1162.         $d -= 1 if $dur->is_preserve_mode;
  1163.  
  1164.         if ( ! $dur->is_wrap_mode && $d > 28 )
  1165.         {
  1166.             # find the rd for the last day of our target month
  1167.             $self->{local_rd_days} = $self->_ymd2rd( $y, $m + $deltas{months} + 1, 0 );
  1168.  
  1169.             # what day of the month is it? (discard year and month)
  1170.             my $last_day = ($self->_rd2ymd( $self->{local_rd_days} ))[2];
  1171.  
  1172.             # if our original day was less than the last day,
  1173.             # use that instead
  1174.             $self->{local_rd_days} -= $last_day - $d if $last_day > $d;
  1175.         }
  1176.         else
  1177.         {
  1178.             $self->{local_rd_days} = $self->_ymd2rd( $y, $m + $deltas{months}, $d );
  1179.         }
  1180.     }
  1181.  
  1182.     if ( $deltas{days} || $deltas{months} )
  1183.     {
  1184.         # We fudge the year so that the calculations being done have
  1185.         # something to work with.
  1186.         $self->{utc_year} += int( $deltas{months} / 12 ) + 1;
  1187.  
  1188.         $self->_calc_utc_rd;
  1189.         $self->_calc_local_rd;
  1190.     }
  1191.  
  1192.     if ( $deltas{minutes} )
  1193.     {
  1194.         $self->{utc_rd_secs} += $deltas{minutes} * 60;
  1195.  
  1196.         # This intentionally ignores leap seconds
  1197.         $self->_normalize_tai_seconds( $self->{utc_rd_days}, $self->{utc_rd_secs} );
  1198.     }
  1199.  
  1200.     # We add seconds to the UTC time because if someone adds 24 hours,
  1201.     # we want this to be _different_ from adding 1 day when crossing
  1202.     # DST boundaries.
  1203.     if ( $deltas{seconds} || $deltas{nanoseconds})
  1204.     {
  1205.         $self->{utc_rd_secs} += $deltas{seconds};
  1206.  
  1207.         if ( $deltas{nanoseconds} )
  1208.         {
  1209.             $self->{rd_nanosecs} += $deltas{nanoseconds};
  1210.             $self->_normalize_nanoseconds( $self->{utc_rd_secs}, $self->{rd_nanosecs} );
  1211.         }
  1212.  
  1213.         # must always normalize seconds, because a nanosecond change
  1214.         # might cause a day change
  1215.         $self->_normalize_seconds;
  1216.     }
  1217.  
  1218.     if ( $deltas{minutes} || $deltas{seconds} || $deltas{nanoseconds})
  1219.     {
  1220.         delete $self->{utc_c};
  1221.         $self->_calc_local_rd;
  1222.     }
  1223.  
  1224.     return $self;
  1225. }
  1226.  
  1227. sub _compare_overload
  1228. {
  1229.     # note: $_[1]->compare( $_[0] ) is an error when $_[1] is not a
  1230.     # DateTime (such as the INFINITY value)
  1231.     return $_[2] ? - $_[0]->compare( $_[1] ) : $_[0]->compare( $_[1] );
  1232. }
  1233.  
  1234. sub compare
  1235. {
  1236.     shift->_compare( @_, 0 );
  1237. }
  1238.  
  1239. sub compare_ignore_floating
  1240. {
  1241.     shift->_compare( @_, 1 );
  1242. }
  1243.  
  1244. sub _compare
  1245. {
  1246.     my ( $class, $dt1, $dt2, $consistent ) = ref $_[0] ? ( undef, @_ ) : @_;
  1247.  
  1248.     return undef unless defined $dt2;
  1249.  
  1250.     if ( ! ref $dt2 && ( $dt2 == INFINITY || $dt2 == NEG_INFINITY ) )
  1251.     {
  1252.         return $dt1->{utc_rd_days} <=> $dt2;
  1253.     }
  1254.  
  1255.     die "Cannot compare a datetime to a regular scalar"
  1256.         unless ( UNIVERSAL::can( $dt1, 'utc_rd_values' ) &&
  1257.                  UNIVERSAL::can( $dt2, 'utc_rd_values' ) );
  1258.  
  1259.     if ( ! $consistent &&
  1260.          UNIVERSAL::can( $dt1, 'time_zone' ) &&
  1261.          UNIVERSAL::can( $dt2, 'time_zone' )
  1262.        )
  1263.     {
  1264.         my $is_floating1 = $dt1->time_zone->is_floating;
  1265.         my $is_floating2 = $dt2->time_zone->is_floating;
  1266.  
  1267.         if ( $is_floating1 && ! $is_floating2 )
  1268.         {
  1269.             $dt1 = $dt1->clone->set_time_zone( $dt2->time_zone );
  1270.         }
  1271.         elsif ( $is_floating2 && ! $is_floating1 )
  1272.         {
  1273.             $dt2 = $dt2->clone->set_time_zone( $dt1->time_zone );
  1274.         }
  1275.     }
  1276.  
  1277.     my @dt1_components = $dt1->utc_rd_values;
  1278.     my @dt2_components = $dt2->utc_rd_values;
  1279.  
  1280.     foreach my $i ( 0..2 )
  1281.     {
  1282.         return $dt1_components[$i] <=> $dt2_components[$i]
  1283.             if $dt1_components[$i] != $dt2_components[$i]
  1284.     }
  1285.  
  1286.     return 0;
  1287. }
  1288.  
  1289. sub _normalize_nanoseconds
  1290. {
  1291.     use integer;
  1292.  
  1293.     # seconds, nanoseconds
  1294.     if ( $_[2] < 0 )
  1295.     {
  1296.         my $overflow = 1 + $_[2] / MAX_NANOSECONDS;
  1297.         $_[2] += $overflow * MAX_NANOSECONDS;
  1298.         $_[1] -= $overflow;
  1299.     }
  1300.     elsif ( $_[2] >= MAX_NANOSECONDS )
  1301.     {
  1302.         my $overflow = $_[2] / MAX_NANOSECONDS;
  1303.         $_[2] -= $overflow * MAX_NANOSECONDS;
  1304.         $_[1] += $overflow;
  1305.     }
  1306. }
  1307.  
  1308. # Many of the same parameters as new() but all of them are optional,
  1309. # and there are no defaults.
  1310. my $SetValidate =
  1311.     { map { my %copy = %{ $BasicValidate->{$_} };
  1312.             delete $copy{default};
  1313.             $copy{optional} = 1;
  1314.             $_ => \%copy }
  1315.       keys %$BasicValidate };
  1316.  
  1317. sub set
  1318. {
  1319.     my $self = shift;
  1320.     my %p = validate( @_, $SetValidate );
  1321.  
  1322.     my %old_p =
  1323.         ( map { $_ => $self->$_() }
  1324.           qw( year month day hour minute second nanosecond locale time_zone )
  1325.         );
  1326.  
  1327.     my $new_dt = (ref $self)->new( %old_p, %p );
  1328.  
  1329.     %$self = %$new_dt;
  1330.  
  1331.     return $self;
  1332. }
  1333.  
  1334. sub truncate
  1335. {
  1336.     my $self = shift;
  1337.     my %p = validate( @_,
  1338.                       { to =>
  1339.                         { regex => qr/^(?:year|month|week|day|hour|minute|second)$/ },
  1340.                       },
  1341.                     );
  1342.  
  1343.     my %new = ( locale    => $self->{locale},
  1344.                 time_zone => $self->{tz},
  1345.               );
  1346.  
  1347.     if ( $p{to} eq 'week' )
  1348.     {
  1349.         my $day_diff = $self->day_of_week - 1;
  1350.  
  1351.         if ($day_diff)
  1352.         {
  1353.             $self->add( days => -1 * $day_diff );
  1354.         }
  1355.  
  1356.         return $self->truncate( to => 'day' );
  1357.     }
  1358.     else
  1359.     {
  1360.     foreach my $f ( qw( year month day hour minute second ) )
  1361.     {
  1362.         $new{$f} = $self->$f();
  1363.  
  1364.         last if $p{to} eq $f;
  1365.     }
  1366.     }
  1367.  
  1368.     my $new_dt = (ref $self)->new(%new);
  1369.  
  1370.     %$self = %$new_dt;
  1371.  
  1372.     return $self;
  1373. }
  1374.  
  1375. sub set_time_zone
  1376. {
  1377.     my ( $self, $tz ) = @_;
  1378.  
  1379.     # This is a bit of a hack but it works because time zone objects
  1380.     # are singletons, and if it doesn't work all we lose is a little
  1381.     # bit of speed.
  1382.     return if $self->{tz} eq $tz;
  1383.  
  1384.     my $was_floating = $self->{tz}->is_floating;
  1385.  
  1386.     $self->{tz} = ref $tz ? $tz : DateTime::TimeZone->new( name => $tz );
  1387.  
  1388.     # if it either was or now is floating (but not both)
  1389.     if ( $self->{tz}->is_floating xor $was_floating )
  1390.     {
  1391.         $self->_calc_utc_rd;
  1392.     }
  1393.     elsif ( ! $was_floating )
  1394.     {
  1395.         $self->_calc_local_rd;
  1396.     }
  1397.  
  1398.     return $self;
  1399. }
  1400.  
  1401. sub STORABLE_freeze
  1402. {
  1403.     my $self = shift;
  1404.     my $cloning = shift;
  1405.  
  1406.     my $serialized = '';
  1407.     foreach my $key ( qw( utc_rd_days
  1408.                           utc_rd_secs
  1409.                           rd_nanosecs ) )
  1410.     {
  1411.         $serialized .= "$key:$self->{$key}|";
  1412.     }
  1413.  
  1414.     # not used yet, but may be handy in the future.
  1415.     $serialized .= "version:$VERSION";
  1416.  
  1417.     return $serialized, $self->{locale}, $self->{tz};
  1418. }
  1419.  
  1420. sub STORABLE_thaw
  1421. {
  1422.     my $self = shift;
  1423.     my $cloning = shift;
  1424.     my $serialized = shift;
  1425.  
  1426.     my %serialized = map { split /:/ } split /\|/, $serialized;
  1427.  
  1428.     my ( $locale, $tz );
  1429.  
  1430.     # more recent code version
  1431.     if (@_)
  1432.     {
  1433.         ( $locale, $tz ) = @_;
  1434.     }
  1435.     else
  1436.     {
  1437.         $tz = DateTime::TimeZone->new( name => delete $serialized{tz} );
  1438.  
  1439.         $locale =
  1440.             DateTime::Locale->load( exists $serialized{language}
  1441.                                     ? delete $serialized{language}
  1442.                                     : delete $serialized{locale}
  1443.                                   );
  1444.     }
  1445.  
  1446.     delete $serialized{version};
  1447.  
  1448.     %$self = %serialized;
  1449.     $self->{tz} = $tz;
  1450.     $self->{locale} = $locale;
  1451.  
  1452.     $self->_calc_local_rd;
  1453.  
  1454.     return $self;
  1455. }
  1456.  
  1457.  
  1458. 1;
  1459.  
  1460. __END__
  1461.  
  1462. =head1 NAME
  1463.  
  1464. DateTime - A date and time object
  1465.  
  1466. =head1 SYNOPSIS
  1467.  
  1468.   use DateTime;
  1469.  
  1470.   $dt = DateTime->new( year   => 1964,
  1471.                        month  => 10,
  1472.                        day    => 16,
  1473.                        hour   => 16,
  1474.                        minute => 12,
  1475.                        second => 47,
  1476.                        nanosecond => 500000000,
  1477.                        time_zone => 'Asia/Taipei',
  1478.                      );
  1479.  
  1480.   $dt = DateTime->from_epoch( epoch => $epoch );
  1481.   $dt = DateTime->now; # same as ( epoch => time() )
  1482.  
  1483.   $year   = $dt->year;
  1484.   $month  = $dt->month;          # 1-12 - also mon
  1485.  
  1486.   $day    = $dt->day;            # 1-31 - also day_of_month, mday
  1487.  
  1488.   $dow    = $dt->day_of_week;    # 1-7 (Monday is 1) - also dow, wday
  1489.  
  1490.   $hour   = $dt->hour;           # 0-23
  1491.   $minute = $dt->minute;         # 0-59 - also min
  1492.  
  1493.   $second = $dt->second;         # 0-61 (leap seconds!) - also sec
  1494.  
  1495.   $doy    = $dt->day_of_year;    # 1-366 (leap years) - also doy
  1496.  
  1497.   $doq    = $dt->day_of_quarter; # 1.. - also doq
  1498.  
  1499.   $qtr    = $dt->quarter;        # 1-4
  1500.  
  1501.   # all of the start-at-1 methods above have correponding start-at-0
  1502.   # methods, such as $dt->day_of_month_0, $dt->month_0 and so on
  1503.  
  1504.   $ymd    = $dt->ymd;           # 2002-12-06
  1505.   $ymd    = $dt->ymd('/');      # 2002/12/06 - also date
  1506.  
  1507.   $mdy    = $dt->mdy;           # 12-06-2002
  1508.   $mdy    = $dt->mdy('/');      # 12/06/2002
  1509.  
  1510.   $dmy    = $dt->dmy;           # 06-12-2002
  1511.   $dmy    = $dt->dmy('/');      # 06/12/2002
  1512.  
  1513.   $hms    = $dt->hms;           # 14:02:29
  1514.   $hms    = $dt->hms('!');      # 14!02!29 - also time
  1515.  
  1516.   $is_leap  = $dt->is_leap_year;
  1517.  
  1518.   # these are localizable, see Locales section
  1519.   $month_name  = $dt->month_name; # January, February, ...
  1520.   $month_abbr  = $dt->month_abbr; # Jan, Feb, ...
  1521.   $day_name    = $dt->day_name;   # Monday, Tuesday, ...
  1522.   $day_abbr    = $dt->day_abbr;   # Mon, Tue, ...
  1523.  
  1524.   $epoch_time  = $dt->epoch;
  1525.   # may return undef if the datetime is outside the range that is
  1526.   # representable by your OS's epoch system.
  1527.  
  1528.   $dt2 = $dt + $duration_object;
  1529.  
  1530.   $dt3 = $dt - $duration_object;
  1531.  
  1532.   $duration_object = $dt - $dt2;
  1533.  
  1534.   $dt->set( year => 1882 );
  1535.  
  1536.   $dt->set_time_zone( 'America/Chicago' );
  1537.  
  1538. =head1 DESCRIPTION
  1539.  
  1540. DateTime is a class for the representation of date/time combinations,
  1541. and is part of the Perl DateTime project.  For details on this project
  1542. please see L<http://datetime.perl.org/>.  The DateTime site has a FAQ
  1543. which may help answer many "how do I do X?" questions.  The FAQ is at
  1544. L<http://datetime.perl.org/faq.html>.
  1545.  
  1546. It represents the Gregorian calendar, extended backwards in time
  1547. before its creation (in 1582).  This is sometimes known as the
  1548. "proleptic Gregorian calendar".  In this calendar, the first day of
  1549. the calendar (the epoch), is the first day of year 1, which
  1550. corresponds to the date which was (incorrectly) believed to be the
  1551. birth of Jesus Christ.
  1552.  
  1553. The calendar represented does have a year 0, and in that way differs
  1554. from how dates are often written using "BCE/CE" or "BC/AD".
  1555.  
  1556. For infinite datetimes, please see the
  1557. L<DateTime::Infinite|DateTime::Infinite> module.
  1558.  
  1559. =head1 USAGE
  1560.  
  1561. =head2 0-based Versus 1-based Numbers
  1562.  
  1563. The DateTime.pm module follows a simple consistent logic for
  1564. determining whether or not a given number is 0-based or 1-based.
  1565.  
  1566. Month, day of month, day of week, and day of year are 1-based.  Any
  1567. method that is 1-based also has an equivalent 0-based method ending in
  1568. "_0".  So for example, this class provides both C<day_of_week()> and
  1569. C<day_of_week_0()> methods.
  1570.  
  1571. The C<day_of_week_0()> method still treats Monday as the first day of
  1572. the week.
  1573.  
  1574. All I<time>-related numbers such as hour, minute, and second are
  1575. 0-based.
  1576.  
  1577. Years are neither, as they can be both positive or negative, unlike
  1578. any other datetime component.  There I<is> a year 0.
  1579.  
  1580. There is no C<quarter_0()> method.
  1581.  
  1582. =head2 Error Handling
  1583.  
  1584. Some errors may cause this module to die with an error string.  This
  1585. can only happen when calling constructor methods, methods that change
  1586. the object, such as C<set()>, or methods that take parameters.
  1587. Methods that retrieve information about the object, such as C<year()>
  1588. or C<epoch()>, will never die.
  1589.  
  1590. =head2 Locales
  1591.  
  1592. Some methods are localizable.  This is done by setting the locale
  1593. when constructing a DateTime object.  There is also a
  1594. C<DefaultLocale()> class method which may be used to set the default
  1595. locale for all DateTime objects created.  If this is not set, then
  1596. "English" is used.
  1597.  
  1598. Some locales may return data as Unicode.  When using Perl 5.6.0 or
  1599. greater, this will be a native Perl Unicode string.  When using older
  1600. Perls, this will be a sequence of bytes representing the Unicode
  1601. character.
  1602.  
  1603. =head2 Floating DateTimes
  1604.  
  1605. The default time zone for all DateTime objects is the "floating" time
  1606. zone.  This concept comes from the iCal standard.  A floating datetime
  1607. is one which is not anchored to any particular time zone.  In
  1608. addition, floating datetimes do not include leap seconds, since we
  1609. cannot use them without knowing the datetime's time zone.
  1610.  
  1611. The results of date math and comparison between a floating datetime
  1612. and one with a real time zone are not really valid, because one
  1613. includes leap seconds and the other does not.  Similarly, the results
  1614. of datetime math between two floating datetimes and two datetimes with
  1615. time zones are not really comparable.
  1616.  
  1617. If you are planning to use any objects with a real time zone, it is
  1618. strongly recommended that you B<do not> mix these with floating
  1619. datetimes.
  1620.  
  1621. =head2 Methods
  1622.  
  1623. =head3 Constructors
  1624.  
  1625. All constructors can die when invalid parameters are given.
  1626.  
  1627. =over 4
  1628.  
  1629. =item * new( ... )
  1630.  
  1631. This class method accepts parameters for each date and time component:
  1632. "year", "month", "day", "hour", "minute", "second", "nanosecond".
  1633. It also accepts "locale" and "time_zone" parameters.
  1634.  
  1635.   my $dt = DateTime->new( year   => 1066,
  1636.                           month  => 10,
  1637.                           day    => 25,
  1638.                           hour   => 7,
  1639.                           minute => 15,
  1640.                           second => 47,
  1641.                           nanosecond => 500000000,
  1642.                           time_zone  => 'America/Chicago',
  1643.                         );
  1644.  
  1645. DateTime validates the "month", "day", "hour", "minute", and "second",
  1646. and "nanosecond" parameters.  The valid values for these parameters are:
  1647.  
  1648. =over 8
  1649.  
  1650. =item * month
  1651.  
  1652. 1-12
  1653.  
  1654. =item * day
  1655.  
  1656. 1-31, and it must be within the valid range of days for the specified
  1657. month
  1658.  
  1659. =item * hour
  1660.  
  1661. 0-23
  1662.  
  1663. =item * minute
  1664.  
  1665. 0-59
  1666.  
  1667. =item * second
  1668.  
  1669. 0-61 (to allow for leap seconds).  Values of 60 or 61 are only allowed
  1670. when they match actual leap seconds.
  1671.  
  1672. =item * nanosecond
  1673.  
  1674. >= 0
  1675.  
  1676. =back
  1677.  
  1678. =back
  1679.  
  1680. Invalid parameter types (like an array reference) will cause the
  1681. constructor to die.
  1682.  
  1683. DateTime does not check if second values greater than 59 are valid
  1684. based on current leap seconds, and invalid values simply cause an
  1685. overflow.
  1686.  
  1687. All of the parameters are optional except for "year".  The "month" and
  1688. "day" parameters both default to 1, while the "hour", "minute", and
  1689. "second", and "nanosecond" parameters all default to 0.
  1690.  
  1691. The locale parameter should be a string matching one of the valid
  1692. locales, or a C<DateTime::Locale> object.  See the
  1693. L<DateTime::Locale|DateTime::Locale> documentation for details.
  1694.  
  1695. The time_zone parameter can be either a scalar or a
  1696. C<DateTime::TimeZone> object.  A string will simply be passed to the
  1697. C<< DateTime::TimeZone->new >> method as its "name" parameter.  This
  1698. string may be an Olson DB time zone name ("America/Chicago"), an
  1699. offset string ("+0630"), or the words "floating" or "local".  See the
  1700. C<DateTime::TimeZone> documentation for more details.
  1701.  
  1702. The default time zone is "floating".
  1703.  
  1704. =head4 Ambiguous Local Times
  1705.  
  1706. Because of Daylight Saving Time, it is possible to specify a local
  1707. time that is ambiguous.  For example, in the US in 2003, the
  1708. transition from to saving to standard time occurs on October 26, at
  1709. 02:00:00 local time.  The local clock changes from 01:59:59 (saving
  1710. time) to 01:00:00 (standard time).  This means that the hour from
  1711. 01:00:00 through 01:59:59 actually occurs twice, though the UTC time
  1712. continues to move forward.
  1713.  
  1714. If you specify an ambiguous time, then the latest UTC time is always
  1715. used, in effect always choosing saving time.  In this case, you can
  1716. simply subtract an hour to the object in order to move to standard time,
  1717. for example:
  1718.  
  1719.   # This object represent 01:30:00 standard time
  1720.   my $dt = DateTime->new( year   => 2003,
  1721.                           month  => 10,
  1722.                           day    => 26,
  1723.                           hour   => 1,
  1724.                           minute => 30,
  1725.                           second => 0,
  1726.                           time_zone => 'America/Chicago',
  1727.                         );
  1728.  
  1729.   print $dt->hms;  # prints 01:30:00
  1730.  
  1731.   # Now the object represent 01:30:00 saving time
  1732.   $dt->subtract( hours => 1 );
  1733.  
  1734.   print $dt->hms;  # still prints 01:30:00
  1735.  
  1736. Alternately, you could create the object with the UTC time zone, and
  1737. then call the C<set_time_zone()> method to change the time zone.  This
  1738. would allow you to unambiguously specify the datetime.
  1739.  
  1740. =head4 Invalid Local Times
  1741.  
  1742. Another problem introduced by Daylight Saving Time is that certain
  1743. local times just do not exist.  For example, in the US in 2003, the
  1744. transition from standard to saving time occurred on April 6, at the
  1745. change to 2:00:00 local time.  The local clock changes from 01:59:59
  1746. (standard time) to 03:00:00 (saving time).  This means that there is
  1747. no 02:00:00 through 02:59:59 on April 6!
  1748.  
  1749. Attempting to create an invalid time currently causes a fatal error.
  1750. This may change in future version of this module.
  1751.  
  1752. =over 4
  1753.  
  1754. =item * from_epoch( epoch => $epoch, ... )
  1755.  
  1756. This class method can be used to construct a new DateTime object from
  1757. an epoch time instead of components.  Just as with the C<new()>
  1758. method, it accepts "time_zone" and "locale" parameters.
  1759.  
  1760. If the epoch value is not an integer, the part after the decimal will
  1761. be converted to nanoseconds.  This is done in order to be compatible
  1762. with C<Time::HiRes>.  If the floating portion extends past 9 decimal
  1763. places, it will be truncated to nine, so that 1.1234567891 will become
  1764. 1 second and 123,456,789 nanoseconds.
  1765.  
  1766. =item * now( ... )
  1767.  
  1768. This class method is equivalent to calling C<from_epoch()> with the
  1769. value returned from Perl's C<time()> function.  Just as with the
  1770. C<new()> method, it accepts "time_zone" and "locale" parameters.
  1771.  
  1772. =item * today( ... )
  1773.  
  1774. This class method is equivalent to:
  1775.  
  1776.   DateTime->now->truncate( to => 'day' );
  1777.  
  1778. =item * from_object( object => $object, ... )
  1779.  
  1780. This class method can be used to construct a new DateTime object from
  1781. any object that implements the C<utc_rd_values()> method.  All
  1782. C<DateTime::Calendar> modules must implement this method in order to
  1783. provide cross-calendar compatibility.  This method accepts a
  1784. "locale" parameter
  1785.  
  1786. If the object passed to this method has a C<time_zone()> method, that
  1787. is used to set the time zone of the newly created C<DateTime.pm>
  1788. object.  Otherwise UTC is used.
  1789.  
  1790. =item * last_day_of_month( ... )
  1791.  
  1792. This constructor takes the same arguments as can be given to the
  1793. C<new()> method, except for "day".  Additionally, both "year" and
  1794. "month" are required.
  1795.  
  1796. =item * from_day_of_year( ... )
  1797.  
  1798. This constructor takes the same arguments as can be given to the
  1799. C<new()> method, except that it does not accept a "month" or "day"
  1800. argument.  Instead, it requires both "year" and "day_of_year".  The
  1801. day of year must be between 1 and 366, and 366 is only allowed for
  1802. leap years.
  1803.  
  1804. =item * clone
  1805.  
  1806. This object method returns a replica of the given object.
  1807.  
  1808. =back
  1809.  
  1810. =head3 "Get" Methods
  1811.  
  1812. This class has many methods for retrieving information about an
  1813. object.
  1814.  
  1815. =over 4
  1816.  
  1817. =item * year
  1818.  
  1819. Returns the year.
  1820.  
  1821. =item * ce_year
  1822.  
  1823. Returns the year according to the BCE/CE numbering system.  The year
  1824. before year 1 in this system is year -1, aka "1 BCE".
  1825.  
  1826. =item * era
  1827.  
  1828. Returns a string, either "BCE" or "CE", according to the year.
  1829.  
  1830. =item * year_with_era
  1831.  
  1832. Returns a string containing the year immediately followed by its era.
  1833. The year is the absolute value of C<ce_year()>, so that year 1 is
  1834. "1CE" and year 0 is "1BCE".
  1835.  
  1836. =item * month
  1837.  
  1838. Returns the month of the year, from 1..12.
  1839.  
  1840. =item * month_name
  1841.  
  1842. Returns the name of the current month.  See the
  1843. L<Locales|/Locales> section for more details.
  1844.  
  1845. =item * month_abbr
  1846.  
  1847. Returns the abbreviated name of the current month.  See the
  1848. L<Locales|/Locales> section for more details.
  1849.  
  1850. =item * day_of_month, day, mday
  1851.  
  1852. Returns the day of the month, from 1..31.
  1853.  
  1854. =item * day_of_week, wday, dow
  1855.  
  1856. Returns the day of the week as a number, from 1..7, with 1 being
  1857. Monday and 7 being Sunday.
  1858.  
  1859. =item * day_name
  1860.  
  1861. Returns the name of the current day of the week.  See the
  1862. L<Locales|/Locales> section for more details.
  1863.  
  1864. =item * day_abbr
  1865.  
  1866. Returns the abbreviated name of the current day of the week.  See the
  1867. L<Locales|/Locales> section for more details.
  1868.  
  1869. =item * day_of_year, doy
  1870.  
  1871. Returns the day of the year.
  1872.  
  1873. =item * quarter
  1874.  
  1875. Returns the quarter of the year, from 1..4.
  1876.  
  1877. =item * day_of_quarter, doq
  1878.  
  1879. Returns the day of the quarter.
  1880.  
  1881. =item * weekday_of_month
  1882.  
  1883. Returns a number from 1..5 indicating which week day of the month this
  1884. is.  For example, June 9, 2003 is the second Monday of the month, and
  1885. so this method returns 2 for that day.
  1886.  
  1887. =item * ymd( $optional_separator ), date
  1888.  
  1889. =item * mdy( $optional_separator )
  1890.  
  1891. =item * dmy( $optional_separator )
  1892.  
  1893. Each method returns the year, month, and day, in the order indicated
  1894. by the method name.  Years are zero-padded to four digits.  Months and
  1895. days are 0-padded to two digits.
  1896.  
  1897. By default, the values are separated by a dash (-), but this can be
  1898. overridden by passing a value to the method.
  1899.  
  1900. =item * hour
  1901.  
  1902. Returns the hour of the day, from 0..23.
  1903.  
  1904. =item * hour_1
  1905.  
  1906. Returns the hour of the day, from 1..24.
  1907.  
  1908. =item * hour_12
  1909.  
  1910. Returns the hour of the day, from 1..12.
  1911.  
  1912. =item * hour_12_0
  1913.  
  1914. Returns the hour of the day, from 0..11.
  1915.  
  1916. =item * minute, min
  1917.  
  1918. Returns the minute of the hour, from 0..59.
  1919.  
  1920. =item * second, sec
  1921.  
  1922. Returns the second, from 0..61.  The values 60 and 61 are used for
  1923. leap seconds.
  1924.  
  1925. =item * fractional_second
  1926.  
  1927. Returns the second, as a real number from 0.0 until 61.999999999
  1928.  
  1929. The values 60 and 61 are used for leap seconds.
  1930.  
  1931. =item * millisecond
  1932.  
  1933. Returns the fractional part of the second as milliseconds (1E-3 seconds).
  1934.  
  1935. Half a second is 500 milliseconds.
  1936.  
  1937. =item * microsecond
  1938.  
  1939. Returns the fractional part of the second as microseconds (1E-6
  1940. seconds).  This value will be rounded to an integer.
  1941.  
  1942. Half a second is 500_000 microseconds.  This value will be rounded to
  1943. an integer.
  1944.  
  1945. =item * nanosecond
  1946.  
  1947. Returns the fractional part of the second as nanoseconds (1E-9 seconds).
  1948.  
  1949. Half a second is 500_000_000 nanoseconds.
  1950.  
  1951. =item * hms( $optional_separator ), time
  1952.  
  1953. Returns the hour, minute, and second, all zero-padded to two digits.
  1954. If no separator is specified, a colon (:) is used by default.
  1955.  
  1956. =item * datetime, iso8601
  1957.  
  1958. This method is equivalent to:
  1959.  
  1960.   $dt->ymd('-') . 'T' . $dt->hms(':')
  1961.  
  1962. =item * is_leap_year
  1963.  
  1964. This method returns a true or false indicating whether or not the
  1965. datetime object is in a leap year.
  1966.  
  1967. =item * week
  1968.  
  1969.  ($week_year, $week_number) = $dt->week;
  1970.  
  1971. Returns information about the calendar week which contains this
  1972. datetime object. The values returned by this method are also available
  1973. separately through the week_year and week_number methods.
  1974.  
  1975. The first week of the year is defined by ISO as the one which contains
  1976. the fourth day of January, which is equivalent to saying that it's the
  1977. first week to overlap the new year by at least four days.
  1978.  
  1979. Typically the week year will be the same as the year that the object
  1980. is in, but dates at the very begining of a calendar year often end up
  1981. in the last week of the prior year, and similarly, the final few days
  1982. of the year may be placed in the first week of the next year.
  1983.  
  1984. =item * week_year
  1985.  
  1986. Returns the year of the week.
  1987.  
  1988. =item * week_number
  1989.  
  1990. Returns the week of the year, from 1..53.
  1991.  
  1992. =item * week_of_month
  1993.  
  1994. The week of the month, from 0..5.  The first week of the month is the
  1995. first week that contains a Thursday.  This is based on the ICU
  1996. definition of week of month, and correlates to the ISO8601 week of
  1997. year definition.  A day in the week I<before> the week with the first
  1998. Thursday will be week 0.
  1999.  
  2000. =item * jd, mjd
  2001.  
  2002. These return the Julian Day and Modified Julian Day, respectively.
  2003. The value returned is a floating point number.  The fractional portion
  2004. of the number represents the time portion of the datetime.
  2005.  
  2006. =item * time_zone
  2007.  
  2008. This returns the C<DateTime::TimeZone> object for the datetime object.
  2009.  
  2010. =item * offset
  2011.  
  2012. This returns the offset, in seconds, of the datetime object according
  2013. to the time zone.
  2014.  
  2015. =item * is_dst
  2016.  
  2017. Returns a boolean indicating whether or not the datetime object is
  2018. currently in Daylight Saving Time or not.
  2019.  
  2020. =item * time_zone_long_name
  2021.  
  2022. This is a shortcut for C<< $dt->time_zone->name >>.  It's provided so
  2023. that one can use "%{time_zone_long_name}" inside as a strftime format
  2024. specifier.
  2025.  
  2026. =item * time_zone_short_name
  2027.  
  2028. This method returns the time zone abbreviation for the current time
  2029. zone, such as "PST" or "GMT".  These names are B<not> definitive, and
  2030. should not be used in any application intended for general use by
  2031. users around the world.
  2032.  
  2033. =item * strftime( $format, ... )
  2034.  
  2035. This method implements functionality similar to the C<strftime()>
  2036. method in C.  However, if given multiple format strings, then it will
  2037. return multiple scalars, one for each format string.
  2038.  
  2039. See the L<strftime Specifiers|/strftime Specifiers> section for a list
  2040. of all possible format specifiers.
  2041.  
  2042. If you give a format specifier that doesn't exist, then it is simply
  2043. treated as text.
  2044.  
  2045. =item * epoch
  2046.  
  2047. Return the UTC epoch value for the datetime object.  Internally, this
  2048. is implemented using C<Time::Local>, which uses the Unix epoch even on
  2049. machines with a different epoch (such as MacOS).  Datetimes before the
  2050. start of the epoch will be returned as a negative number.
  2051.  
  2052. This return value from this method is always an integer.
  2053.  
  2054. Since the epoch does not account for leap seconds, the epoch time for
  2055. 1971-12-31T23:59:60 (UTC) is exactly the same as that for
  2056. 1972-01-01T00:00:00.
  2057.  
  2058. Epoch times cannot represent many dates on most platforms, and this
  2059. method may simply return undef in some cases.
  2060.  
  2061. Using your system's epoch time may be error-prone, since epoch times
  2062. have such a limited range on 32-bit machines.  Additionally, the fact
  2063. that different operating systems have different epoch beginnings is
  2064. another source of possible bugs.
  2065.  
  2066. =item * hires_epoch
  2067.  
  2068. Returns the epoch as a floating point number.  The floating point
  2069. portion of the value represents the nanosecond value of the object.
  2070. This method is provided for compatibility with the C<Time::HiRes>
  2071. module.
  2072.  
  2073. =item * is_finite, is_infinite
  2074.  
  2075. These methods allow you to distinguish normal datetime objects from
  2076. infinite ones.  Infinite datetime objects are documented in
  2077. L<DateTime::Infinite|DateTime::Infinite>.
  2078.  
  2079. =item * utc_rd_values
  2080.  
  2081. Returns the current UTC Rata Die days, seconds, and nanoseconds as a
  2082. three element list.  This exists primarily to allow other calendar
  2083. modules to create objects based on the values provided by this object.
  2084.  
  2085. =item * leap_seconds
  2086.  
  2087. Returns the number of leap seconds that have happened up to the
  2088. datetime represented by the object.  For floating datetimes, this
  2089. always returns 0.
  2090.  
  2091. =item * utc_rd_as_seconds
  2092.  
  2093. Returns the current UTC Rata Die days and seconds purely as seconds.
  2094. This number ignores any fractional seconds stored in the object,
  2095. as well as leap seconds.
  2096.  
  2097. =item * local_rd_as_seconds - deprecated
  2098.  
  2099. Returns the current local Rata Die days and seconds purely as seconds.
  2100. This number ignores any fractional seconds stored in the object,
  2101. as well as leap seconds.
  2102.  
  2103. =item * locale
  2104.  
  2105. Returns the current locale object.
  2106.  
  2107. =back
  2108.  
  2109. =head3 "Set" Methods
  2110.  
  2111. The remaining methods provided by C<DateTime.pm>, except where otherwise
  2112. specified, return the object itself, thus making method chaining
  2113. possible. For example:
  2114.  
  2115.   my $dt = DateTime->now->set_time_zone( 'Australia/Sydney' );
  2116.  
  2117.   my $first = DateTime
  2118.                 ->last_day_of_month( year => 2003, month => 3 )
  2119.                 ->add( days => 1 )
  2120.                 ->subtract( seconds => 1 );
  2121.  
  2122. =over 4
  2123.  
  2124. =item * set( .. )
  2125.  
  2126. This method can be used to change the local components of a date time,
  2127. or its locale.  This method accepts any parameter allowed by the
  2128. C<new()> method except for "time_zone".  Time zones may be set using
  2129. the C<set_time_zone()> method.
  2130.  
  2131. This method performs parameters validation just as is done in the
  2132. C<new()> method.
  2133.  
  2134. =item * truncate( to => ... )
  2135.  
  2136. This method allows you to reset some of the local time components in
  2137. the object to their "zero" values.  The "to" parameter is used to
  2138. specify which values to truncate, and it may be one of "year",
  2139. "month", "week", "day", "hour", "minute", or "second".  For example,
  2140. if "month" is specified, then the local day becomes 1, and the hour,
  2141. minute, and second all become 0.
  2142.  
  2143. If "week" is given, then the datetime is set to the beginning of the
  2144. week in which it occurs, and the time components are all set to 0.
  2145.  
  2146. =item * set_time_zone( $tz )
  2147.  
  2148. This method accepts either a time zone object or a string that can be
  2149. passed as the "name" parameter to C<< DateTime::TimeZone->new() >>.
  2150. If the new time zone's offset is different from the old time zone,
  2151. then the I<local> time is adjusted accordingly.
  2152.  
  2153. For example:
  2154.  
  2155.   my $dt = DateTime->new( year => 2000, month => 5, day => 10,
  2156.                           hour => 15, minute => 15,
  2157.                           time_zone => 'America/Los_Angeles', );
  2158.  
  2159.   print $dt->hour; # prints 15
  2160.  
  2161.   $dt->set_time_zone( 'America/Chicago' );
  2162.  
  2163.   print $dt->hour; # prints 17
  2164.  
  2165. If the old time zone was a floating time zone, then no adjustments to
  2166. the local time are made, except to account for leap seconds.  If the
  2167. new time zone is floating, then the I<UTC> time is adjusted in order
  2168. to leave the local time untouched.
  2169.  
  2170. Fans of Tsai Ming-Liang's films will be happy to know that this does
  2171. work:
  2172.  
  2173.   my $dt = DateTime::TimeZone->now( time_zone => 'Asia/Taipei' );
  2174.  
  2175.   $dt->set_time_zone( 'Europe/Paris' );
  2176.  
  2177. Yes, now we can know "ni3 na4 bian1 ji3dian2?"
  2178.  
  2179. =item * add_duration( $duration_object )
  2180.  
  2181. This method adds a C<DateTime::Duration> to the current datetime.  See
  2182. the L<DateTime::Duration|DateTime::Duration> docs for more details.
  2183.  
  2184. =item * add( DateTime::Duration->new parameters )
  2185.  
  2186. This method is syntactic sugar around the C<add_duration()> method.  It
  2187. simply creates a new C<DateTime::Duration> object using the parameters
  2188. given, and then calls the C<add_duration()> method.
  2189.  
  2190. =item * subtract_duration( $duration_object )
  2191.  
  2192. When given a C<DateTime::Duration> object, this method simply calls
  2193. C<invert()> on that object and passes that new duration to the
  2194. C<add_duration> method.
  2195.  
  2196. =item * subtract( DateTime::Duration->new parameters )
  2197.  
  2198. Like C<add()>, this is syntactic sugar for the C<subtract_duration()>
  2199. method.
  2200.  
  2201. =item * subtract_datetime( $datetime )
  2202.  
  2203. This method returns a new C<DateTime::Duration> object representing
  2204. the difference between the two dates.  The duration is B<relative> to
  2205. the object from which C<$datetime> is subtracted.  For example:
  2206.  
  2207.     2003-03-15 00:00:00.00000000
  2208.  -  2003-02-15 00:00:00.00000000
  2209.  
  2210.  -------------------------------
  2211.  
  2212.  = 1 month
  2213.  
  2214. Note that this duration is not an absolute measure of the amount of
  2215. time between the two datetimes, because the length of a month varies
  2216. by month, as well as the presence of leap seconds.
  2217.  
  2218. The returned duration may have deltas for months, days, minutes,
  2219. seconds, and nanoseconds.
  2220.  
  2221. =item * subtract_datetime_absolute( $datetime )
  2222.  
  2223. This method returns a new C<DateTime::Duration> object representing
  2224. the difference between the two dates.  The duration object will only
  2225. have deltas for seconds and nanoseconds.  This is the only way to
  2226. accurately measure the absolute amount of time between two datetimes,
  2227. since units larger than a second do not represent a fixed number of
  2228. seconds.
  2229.  
  2230. =item * delta_md( $datetime )
  2231.  
  2232. =item * delta_days( $datetime )
  2233.  
  2234. =item * delta_ms( $datetime )
  2235.  
  2236. Each of these methods returns a new C<DateTime::Duration> object
  2237. representing some portion of the difference between two datetimes.
  2238. The C<delta_md()> method returns a duration which contains only the
  2239. month and day portions of the duration is represented.  The
  2240. C<delta_days()> method returns a duration which contains only days,
  2241. and the C<delta_ms()> method returns a duration which contains only
  2242. minutes and seconds.
  2243.  
  2244. The C<delta_md> and C<delta_days> methods truncate the duration so
  2245. that any fractional portion of a day is ignored.  The C<delta_ms>
  2246. method converts any day and months differences to minutes.
  2247.  
  2248. Unlike the subtraction methods, B<these methods always return a
  2249. positive (or zero) duration>.
  2250.  
  2251. =back
  2252.  
  2253. =head3 Class Methods
  2254.  
  2255. =over 4
  2256.  
  2257. =item * DefaultLocale( $locale )
  2258.  
  2259. This can be used to specify the default locale to be used when
  2260. creating DateTime objects.  If unset, then "en_US" is used.
  2261.  
  2262. =item * compare
  2263.  
  2264. =item * compare_ignore_floating
  2265.  
  2266.   $cmp = DateTime->compare( $dt1, $dt2 );
  2267.  
  2268.   $cmp = DateTime->compare_ignore_floating( $dt1, $dt2 );
  2269.  
  2270. Compare two DateTime objects.  The semantics are compatible with
  2271. Perl's C<sort()> function; it returns -1 if $a < $b, 0 if $a == $b, 1
  2272. if $a > $b.
  2273.  
  2274. If one of the two DateTime objects has a floating time zone, it will
  2275. first be converted to the time zone of the other object.  This is what
  2276. you want most of the time, but it can lead to inconsistent results
  2277. when you compare a number of DateTime objects, some of which are
  2278. floating, and some of which are in other time zones.
  2279.  
  2280. If you want to have consistent results (because you want to sort a
  2281. number of objects, for example), you can use the
  2282. C<compare_ignore_floating()> method:
  2283.  
  2284.   @dates = sort { DateTime->compare_ignore_floating($a, $b) } @dates;
  2285.  
  2286. In this case, objects with a floating time zone will be sorted as if
  2287. they were UTC times.
  2288.  
  2289. Since DateTime objects overload comparison operators, this:
  2290.  
  2291.   @dates = sort @dates;
  2292.  
  2293. is equivalent to this:
  2294.  
  2295.   @dates = sort { DateTime->compare($a, $b) } @dates;
  2296.  
  2297. DateTime objects can be compared to any other calendar class that
  2298. implements the C<utc_rd_values()> method.
  2299.  
  2300. =back
  2301.  
  2302. =head2 How Date Math is Done
  2303.  
  2304. It's important to have some understanding of how date math is
  2305. implemented in order to effectively use this module and
  2306. C<DateTime::Duration>.
  2307.  
  2308. The parts of a duration can be broken down into four parts.  These are
  2309. months, days, minutes, and seconds.  Adding one month to a date is
  2310. different than adding 4 weeks or 28, 29, 30, or 31 days.  Similarly,
  2311. due to DST and leap seconds, adding a day can be different than adding
  2312. 86,400 seconds, and adding a minute is not exactly the same as 60
  2313. seconds.
  2314.  
  2315. C<DateTime.pm> always adds (or subtracts) days, then months, minutes,
  2316. and then seconds.  If there are any boundary overflows, these are
  2317. normalized at each step.
  2318.  
  2319. This means that adding one month and one day to February 28, 2003 will
  2320. produce the date April 1, 2003, not March 29, 2003.
  2321.  
  2322.   my $dt = DateTime->new( year => 2003, month => 2, day => 28 );
  2323.  
  2324.   $dt->add( months => 1, days => 1 );
  2325.  
  2326.   # 2003-04-01 - the result
  2327.  
  2328. On the other hand, if we add months first, and then separately add
  2329. days, we end up with March 29, 2003:
  2330.  
  2331.   $dt->add( months => 1 )->add( days => 1 );
  2332.  
  2333.   # 2003-03-29
  2334.  
  2335. =head3 Leap Seconds and Date Math
  2336.  
  2337. The presence of leap seconds can cause some strange anomalies in date
  2338. math.  For example, the following is a legal datetime:
  2339.  
  2340.   my $dt = DateTime->new( year => 1971, month => 12, day => 31,
  2341.                           hour => 23, minute => 59, second => 60,
  2342.                           time_zone => 'UTC' );
  2343.  
  2344. If we do the following:
  2345.  
  2346.  $dt->add( months => 1 );
  2347.  
  2348. Then the datetime is now "1972-02-01 00:00:00", because there is no
  2349. 23:59:60 on 1972-01-31.
  2350.  
  2351. Leap seconds also force us to distinguish between minutes and seconds
  2352. during date math.  Given the following datetime:
  2353.  
  2354.   my $dt = DateTime->new( year => 1971, month => 12, day => 31,
  2355.                           hour => 23, minute => 59, second => 30,
  2356.                           time_zone => 'UTC' );
  2357.  
  2358. we will get different results when adding 1 minute than we get if we
  2359. add 60 seconds.  This is because in this case, the last minute of the
  2360. day, beginning at 23:59:00, actually contains 61 seconds.
  2361.  
  2362. Here are the results we get:
  2363.  
  2364.   # 1971-12-31 23:59:30 - our starting datetime
  2365.  
  2366.   $dt->clone->add( minutes => 1 );
  2367.   # 1972-01-01 00:00:30 - one minute later
  2368.  
  2369.   $dt->clone->add( seconds => 60 );
  2370.   # 1972-01-01 00:00:29 - 60 seconds later
  2371.  
  2372. =head3 Local vs. UTC and 24 hours vs. 1 day
  2373.  
  2374. When doing date math, you are changing the I<local> datetime.  This is
  2375. generally the same as changing the UTC datetime, except when a change
  2376. crosses a daylight saving boundary.  The net effect of this is that a
  2377. single day may have more or less than 24.
  2378.  
  2379. Specifically, if you do this:
  2380.  
  2381.   my $dt = DateTime->new( year => 2003, month => 4, day => 5,
  2382.                           hour => 2,
  2383.                           time_zone => 'America/Chicago',
  2384.                         );
  2385.   $dt->add( days => 1 );
  2386.  
  2387. then you will produce an I<invalid> local time, and therefore an
  2388. exception will be thrown.
  2389.  
  2390. However, this works:
  2391.  
  2392.   my $dt = DateTime->new( year => 2003, month => 4, day => 5,
  2393.                           hour => 2,
  2394.                           time_zone => 'America/Chicago',
  2395.                         );
  2396.   $dt->add( hours => 24 );
  2397.  
  2398. and produces a datetime with the local time of "03:00".
  2399.  
  2400. Another way of thinking of this is that when doing date math, each of
  2401. the seconds, minutes, days, and months components is added separately
  2402. to the local time.
  2403.  
  2404. So when we add 1 day to "2003-02-22 12:00:00" we are incrementing day
  2405. component, 22, by one in order to produce 23.  If we add 24 hours,
  2406. however, we're adding "24 * 60" minutes to the time component, and
  2407. then normalizing the result (because there is no "36:00:00").
  2408.  
  2409. If all this makes your head hurt, there is a simple alternative.  Just
  2410. convert your datetime object to the "UTC" time zone before doing date
  2411. math on it, and switch it back to the local time zone afterwards.
  2412. This avoids the possibility of having date math throw an exception,
  2413. and makes sure that 1 day equals 24 hours.  Of course, this may not
  2414. always be desirable, so caveat user!
  2415.  
  2416. =head3 The Results of Date Math
  2417.  
  2418. Because date math is done on each unit separately, the results of date
  2419. math may not always be what you expect.
  2420.  
  2421. Internally, a duration is made up internally of several different
  2422. units, months, days, minutes, seconds, and nanoseconds.
  2423.  
  2424. Of those, the only ones that convert or normalize to other units are
  2425. seconds <=> nanoseconds.  For all the others, there is no fixed
  2426. conversion between the two units, because of things like leap seconds,
  2427. DST changes, etc.
  2428.  
  2429. Here's an example, based on a question from Mark Fowler to the
  2430. datetime@perl.org list.
  2431.  
  2432. If you want to know how many seconds a duration really represents, you
  2433. have to add it to a datetime to find out, so you could do:
  2434.  
  2435.  my $now = DateTime->now( time_zone => 'UTC' );
  2436.  my $later = $now->clone->add_duration($duration);
  2437.  
  2438.  my $seconds_dur = $later->subtract_datetime_absolute($now);
  2439.  
  2440. This returns a duration which only contains seconds and nanoseconds.
  2441. There are other subtract/delta methods in DateTime.pm to generate
  2442. different types of durations.  These methods are
  2443. C<subtract_datetime()>, C<subtract_datetime_absolute()>,
  2444. C<delta_md()>, L<delta_days()>, and C<delta_ms()>.
  2445.  
  2446. =head2 Overloading
  2447.  
  2448. This module explicitly overloads the addition (+), subtraction (-),
  2449. string and numeric comparison operators.  This means that the
  2450. following all do sensible things:
  2451.  
  2452.   my $new_dt = $dt + $duration_obj;
  2453.  
  2454.   my $new_dt = $dt - $duration_obj;
  2455.  
  2456.   my $duration_obj = $dt - $new_dt;
  2457.  
  2458.   foreach my $dt ( sort @dts ) { ... }
  2459.  
  2460. Additionally, the fallback parameter is set to true, so other
  2461. derivable operators (+=, -=, etc.) will work properly.  Do not expect
  2462. increment (++) or decrement (--) to do anything useful.
  2463.  
  2464. The module also overloads stringification to use the C<iso8601()>
  2465. method.
  2466.  
  2467. =head2 strftime Specifiers
  2468.  
  2469. The following specifiers are allowed in the format string given to the
  2470. C<strftime()> method:
  2471.  
  2472. =over 4
  2473.  
  2474. =item * %a
  2475.  
  2476. The abbreviated weekday name.
  2477.  
  2478. =item * %A
  2479.  
  2480. The full weekday name.
  2481.  
  2482. =item * %b
  2483.  
  2484. The abbreviated month name.
  2485.  
  2486. =item * %B
  2487.  
  2488. The full month name.
  2489.  
  2490. =item * %c
  2491.  
  2492. The default datetime format for the object's locale.
  2493.  
  2494. =item * %C
  2495.  
  2496. The century number (year/100) as a 2-digit integer.
  2497.  
  2498. =item * %d
  2499.  
  2500. The day of the month as a decimal number (range 01 to 31).
  2501.  
  2502. =item * %D
  2503.  
  2504. Equivalent to %m/%d/%y.  This is not a good standard format if you
  2505. have want both Americans and Europeans to understand the date!
  2506.  
  2507. =item * %e
  2508.  
  2509. Like %d, the day of the month as a decimal number, but a leading zero
  2510. is replaced by a space.
  2511.  
  2512. =item * %F
  2513.  
  2514. Equivalent to %Y-%m-%d (the ISO 8601 date format)
  2515.  
  2516. =item * %G
  2517.  
  2518. The ISO 8601 year with century as a decimal number.  The 4-digit year
  2519. corresponding to the ISO week number (see %V).  This has the same
  2520. format and value as %y, except that if the ISO week number belongs to
  2521. the previous or next year, that year is used instead. (TZ)
  2522.  
  2523. =item * %g
  2524.  
  2525. Like %G, but without century, i.e., with a 2-digit year (00-99).
  2526.  
  2527. =item * %h
  2528.  
  2529. Equivalent to %b.
  2530.  
  2531. =item * %H
  2532.  
  2533. The hour as a decimal number using a 24-hour clock (range 00 to 23).
  2534.  
  2535. =item * %I
  2536.  
  2537. The hour as a decimal number using a 12-hour clock (range 01 to 12).
  2538.  
  2539. =item * %j
  2540.  
  2541. The day of the year as a decimal number (range 001 to 366).
  2542.  
  2543. =item * %k
  2544.  
  2545. The hour (24-hour clock) as a decimal number (range 0 to 23); single
  2546. digits are preceded by a blank. (See also %H.)
  2547.  
  2548. =item * %l
  2549.  
  2550. The hour (12-hour clock) as a decimal number (range 1 to 12); single
  2551. digits are preceded by a blank. (See also %I.)
  2552.  
  2553. =item * %m
  2554.  
  2555. The month as a decimal number (range 01 to 12).
  2556.  
  2557. =item * %M
  2558.  
  2559. The minute as a decimal number (range 00 to 59).
  2560.  
  2561. =item * %n
  2562.  
  2563. A newline character.
  2564.  
  2565. =item * %N
  2566.  
  2567. The fractional seconds digits. Default is 9 digits (nanoseconds).
  2568.  
  2569.   %3N   milliseconds (3 digits)
  2570.   %6N   microseconds (6 digits)
  2571.   %9N   nanoseconds  (9 digits)
  2572.  
  2573. =item * %p
  2574.  
  2575. Either `AM' or `PM' according to the given time value, or the
  2576. corresponding strings for the current locale.  Noon is treated as `pm'
  2577. and midnight as `am'.
  2578.  
  2579. =item * %P
  2580.  
  2581. Like %p but in lowercase: `am' or `pm' or a corresponding string for
  2582. the current locale.
  2583.  
  2584. =item * %r
  2585.  
  2586. The time in a.m.  or p.m. notation.  In the POSIX locale this is
  2587. equivalent to `%I:%M:%S %p'.
  2588.  
  2589. =item * %R
  2590.  
  2591. The time in 24-hour notation (%H:%M). (SU) For a version including the
  2592. seconds, see %T below.
  2593.  
  2594. =item * %s
  2595.  
  2596. The number of seconds since the epoch.
  2597.  
  2598. =item * %S
  2599.  
  2600. The second as a decimal number (range 00 to 61).
  2601.  
  2602. =item * %t
  2603.  
  2604. A tab character.
  2605.  
  2606. =item * %T
  2607.  
  2608. The time in 24-hour notation (%H:%M:%S).
  2609.  
  2610. =item * %u
  2611.  
  2612. The day of the week as a decimal, range 1 to 7, Monday being 1.  See
  2613. also %w.
  2614.  
  2615. =item * %U
  2616.  
  2617. The week number of the current year as a decimal number, range 00 to
  2618. 53, starting with the first Sunday as the first day of week 01. See
  2619. also %V and %W.
  2620.  
  2621. =item * %V
  2622.  
  2623. The ISO 8601:1988 week number of the current year as a decimal number,
  2624. range 01 to 53, where week 1 is the first week that has at least 4
  2625. days in the current year, and with Monday as the first day of the
  2626. week. See also %U and %W.
  2627.  
  2628. =item * %w
  2629.  
  2630. The day of the week as a decimal, range 0 to 6, Sunday being 0.  See
  2631. also %u.
  2632.  
  2633. =item * %W
  2634.  
  2635. The week number of the current year as a decimal number, range 00 to
  2636. 53, starting with the first Monday as the first day of week 01.
  2637.  
  2638. =item * %x
  2639.  
  2640. The default date format for the object's locale.
  2641.  
  2642. =item * %X
  2643.  
  2644. The default time format for the object's locale.
  2645.  
  2646. =item * %y
  2647.  
  2648. The year as a decimal number without a century (range 00 to 99).
  2649.  
  2650. =item * %Y
  2651.  
  2652. The year as a decimal number including the century.
  2653.  
  2654. =item * %z
  2655.  
  2656. The time-zone as hour offset from UTC.  Required to emit
  2657. RFC822-conformant dates (using "%a, %d %b %Y %H:%M:%S %z").
  2658.  
  2659. =item * %Z
  2660.  
  2661. The time zone or name or abbreviation.
  2662.  
  2663. =item * %%
  2664.  
  2665. A literal `%' character.
  2666.  
  2667. =item * %{method}
  2668.  
  2669. Any method name may be specified using the format C<%{method}> name
  2670. where "method" is a valid C<DateTime.pm> object method.
  2671.  
  2672. =back
  2673.  
  2674. =head1 DateTime.pm and Storable
  2675.  
  2676. As of version 0.13, DateTime implements Storable hooks in order to
  2677. reduce the size of a serialized DateTime object.
  2678.  
  2679. =head1 SUPPORT
  2680.  
  2681. Support for this module is provided via the datetime@perl.org email
  2682. list.  See http://lists.perl.org/ for more details.
  2683.  
  2684. Please submit bugs to the CPAN RT system at
  2685. http://rt.cpan.org/NoAuth/ReportBug.html?Queue=datetime or via email
  2686. at bug-datetime@rt.cpan.org.
  2687.  
  2688. =head1 AUTHOR
  2689.  
  2690. Dave Rolsky <autarch@urth.org>
  2691.  
  2692. However, please see the CREDITS file for more details on who I really
  2693. stole all the code from.
  2694.  
  2695. =head1 COPYRIGHT
  2696.  
  2697. Copyright (c) 2003 David Rolsky.  All rights reserved.  This program
  2698. is free software; you can redistribute it and/or modify it under the
  2699. same terms as Perl itself.
  2700.  
  2701. Portions of the code in this distribution are derived from other
  2702. works.  Please see the CREDITS file for more details.
  2703.  
  2704. The full text of the license can be found in the LICENSE file included
  2705. with this module.
  2706.  
  2707. =head1 SEE ALSO
  2708.  
  2709. datetime@perl.org mailing list
  2710.  
  2711. http://datetime.perl.org/
  2712.  
  2713. =cut
  2714.